package com.hjqjl.module_home.ui;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;

import com.hjqjl.module_home.R;
import com.hjqjl.module_home.databinding.HomeActivitySpinnerBinding;
import com.hjqjl.whapplib.base.BaseActivity;

public class SpinnerActivity extends BaseActivity<HomeActivitySpinnerBinding> {
    public static void start(Context context) {
        Intent starter = new Intent(context, SpinnerActivity.class);
        context.startActivity(starter);
    }

    ArrayAdapter<String> provinceAdapter = null; // 省级适配器
    ArrayAdapter<String> cityAdapter = null; // 地级适配器
    ArrayAdapter<String> countyAdapter = null; // 县级适配器
    private int provincePosition = 3;

    //我的数据是用数组模拟的，实际项目中我是从服务器拉取的数据
    // 省级选项值
    private String[] province = new String[]{"北京", "上海", "天津", "广东"};
    // 地级选项值
    private String[][] city = new String[][]{{"东城区", "西城区", "崇文区"},// 北京
            {"长宁区", "静安区", "普陀区", "闸北区", "虹口区"},// 上海
            {"和平区", "河东区", "河西区", "南开区", "河北区", "红桥区", "塘沽区", "汉沽区", "大港区", "东丽区"},// 天津
            {"广州", "深圳", "韶关"} // 广东
    };

    // 县级选项值
    private String[][][] county = new String[][][]{{{"无"}, {"无"}, {"无"}},// 北京
            {{"无"}, {"无"}, {"无"}, {"无"}, {"无"}},// 上海
            {{"无"}, {"无"}, {"无"}, {"无"}, {"无"}, {"无"}, {"无"}, {"无"}, {"无"}, {"无"}},// 天津
            {{"海珠区", "荔湾区"}, // 广州
                    {"宝安区", "福田区", "龙岗区"}, // 深圳
                    {"武江区", "浈江区", "曲江区", "乐昌市"} // 韶关
            } // 广东
    };


    @Override
    public void initData() {
        setTitle("Spinner");
        setSpinner();
    }

    @Override
    public void initListener() {
        // 省级下拉框监听
        pVBinding.homeSpSpinnerProvince.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            // 表示选项被改变的时候触发此方法，主要实现办法：动态改变地级适配器的绑定值
            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) {
                // position为当前省级选中的值的序号

                // 将地级适配器的值改变为city[position]中的值
                cityAdapter = new ArrayAdapter<>(pContext, android.R.layout.simple_spinner_item,
                        city[position]);
                // 设置二级下拉列表的选项内容适配器
                pVBinding.homeSpSpinnerCity.setAdapter(cityAdapter);
                provincePosition = position; // 记录当前省级序号，留给下面修改县级适配器时用
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {

            }

        });

        // 地级下拉监听
        pVBinding.homeSpSpinnerCity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) {
                countyAdapter = new ArrayAdapter<>(pContext, android.R.layout.simple_spinner_item,
                        county[provincePosition][position]);
                pVBinding.homeSpSpinnerCounty.setAdapter(countyAdapter);
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {

            }
        });
    }

    /*
     * 设置下拉框
     */
    private void setSpinner() {
        // 绑定适配器和值 //spinner_item_layout是自定义的布局，好看
        provinceAdapter = new ArrayAdapter<>(pContext, R.layout.home_item_spinner_province, province);
        pVBinding.homeSpSpinnerProvince.setAdapter(provinceAdapter);
        pVBinding.homeSpSpinnerProvince.setSelection(3, true); // 设置默认选中项，此处为默认选中第4个值

        //simple_spinner_item是系统自带的布局，好丑
        cityAdapter = new ArrayAdapter<>(pContext, android.R.layout.simple_spinner_item, city[3]);//city[3]设置是根据provinceSpinner设置的
        pVBinding.homeSpSpinnerCity.setAdapter(cityAdapter);
        pVBinding.homeSpSpinnerCity.setSelection(0, true); // 默认选中第1个

        countyAdapter = new ArrayAdapter<>(pContext, android.R.layout.simple_spinner_item, county[3][0]);//county[3][0]是根据cityAdapter设置的
        pVBinding.homeSpSpinnerCounty.setAdapter(countyAdapter);
        pVBinding.homeSpSpinnerCounty.setSelection(0, true);
    }
}
